<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.consume_remark_004"
        placeholder="用户名称"
        clearable
        class="filter-item input-item"
        style="width: 200px"
        @clear="handleFilter"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.id_public_consume_record"
        placeholder="提现单号"
        clearable
        class="filter-item input-item"
        style="width: 200px"
        @clear="handleFilter"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <div class="page-content">
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column label="提现单号" align="center" width="180px">
          <template slot-scope="{ row }">
            <span class="text-desc consume-id">{{
              row.id_public_consume_record
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户名称" align="center">
          <template slot-scope="{ row }">
            <span class="text-desc">{{ row.consume_remark_004 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center">
          <template slot-scope="{ row }">
            <span class="text-desc">{{ row.consume_remark_005 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="提现至" align="center">
          <template>
            <span class="text-desc">微信</span>
          </template>
        </el-table-column>
        <el-table-column label="提现金额" align="center">
          <template slot-scope="{ row }">
            <span class="text-desc">{{ Math.abs(row.consume_price) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" width="160px">
          <template slot-scope="{ row }">
            <span>{{ row.consume_remark_006 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核时间" align="center" width="160px">
          <template slot-scope="{ row }">
            <span>{{ row.consume_remark_002 || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="{ row }">
            <span>{{
              row.consume_status === 901
                ? "待审批"
                : row.consume_status === 902
                ? "审批拒绝"
                : "审批通过"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-button
              :disabled="row.consume_status !== 901"
              type="primary"
              size="mini"
              @click="handleOKorNOT(row)"
            >
              审核
            </el-button>
            <!-- <el-button type="primary" size="mini" @click="handleDetails(row)">
              详情
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="currentPage"
      :limit.sync="listQuery.limit"
      @pagination="getPageList"
    />

    <el-dialog title="提现审批" :visible.sync="dialogFormVisible" width="28%">
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="left"
        label-width="80px"
      >
        <el-row :gutter="40">
          <el-col>
            <el-radio-group v-model="temp.consume_status">
              <el-radio :label="temp.isOK">同意提现</el-radio>
              <el-radio :label="temp.notOK">不同意</el-radio>
            </el-radio-group>
          </el-col>
        </el-row>
        <el-row :gutter="40" style="margin-top: 20px">
          <el-col>
            <el-input
              v-model="temp.consume_remark_003"
              placeholder="请填写审批备注"
            />
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog"> 取消 </el-button>
        <el-button type="primary" @click="detailRefund(temp)"> 确认 </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination";
import { userList, consumeList } from "@/api/user"; // secondary package based on el-pagination
import { warehouseList } from "@/api/warehouse";
import { middleList } from "@/api/middle";
import { formatDate } from "@/utils/tool";
export default {
  name: "History",
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      currentPage: 0,
      listLoading: true,
      shopData: [],
      listQuery: {
        invoke_method: "public_page_4m_consume_list",
        pre_db_para_begin_idx: 0,
        pre_db_para_length_4_list: 10,
        seller_id: 99101,
        consume_type: 201,
        pre_db_para_order_by_para: "consume_remark_006 DESC",
      },
      temp: {
        id: "",
        consume_status: 903,
        consume_remark_003: "",
        isOK: 903,
        notOK: 902,
        consume_price: "",
      },
      dialogFormVisible: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /*
     * 获取页面数据
     * */
    getList() {
      this.listLoading = true;
      consumeList(this.Qs.stringify(this.listQuery)).then((response) => {
        this.list = response.page_content;
        console.log(this.list);
        this.total = response.result_num;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 0.5 * 1000);
      });
    },
    /*
     * 点击分页进行查询
     * */
    getPageList(val) {
      this.listQuery.pre_db_para_begin_idx =
        (val.page - 1) * this.listQuery.pre_db_para_length_4_list;
      this.getList();
    },
    /*
     * 查询数据
     * */
    handleFilter() {
      this.listQuery.pre_db_para_begin_idx = 0;
      this.getList();
    },
    cancelDialog() {
      this.dialogFormVisible = false;
    },
    // 审批
    handleOKorNOT(row) {
      this.temp.id = row.id_public_consume_record;
      this.temp.consume_price = row.consume_price;
      this.dialogFormVisible = true;
    },
    detailRefund(temp) {
      let time = new Date();
      time = formatDate(time, "YYYY.MM.DD hh:mm:ss");
      if (this.temp.consume_status === 903) {
        consumeList(
          this.Qs.stringify({
            invoke_method: "public_page_4m_allow_cash_change",
            id_public_consume_record: this.temp.id,
            consume_remark: "同意提现",
            pre_cmd_para_empower_code: 123,
            pre_cmd_para_money_type: "type_company",
            pre_cmd_para_offline: false,
            consume_price: this.temp.consume_price,
            consume_remark_002: time,
          })
        ).then(() => {
          this.$notify({
            title: "Success",
            message: "审批通过",
            type: "success",
            duration: 2000,
          });
          this.getList();
          this.dialogFormVisible = false;
        });
        // });
      } else {
        consumeList(
          this.Qs.stringify({
            invoke_method: "public_page_4m_insert_or_upd_consume_record",
            pre_db_para_id_public_consume_record: this.temp.id,
            consume_remark: "拒绝用户提现",
            is_online: 0,
            consume_status: 902,
            /*pre_cmd_para_check_exist_apply: true,*/
            consume_remark_002: time,
            consume_remark_003: this.temp.consume_remark_003,
          })
        ).then(() => {
          this.$notify({
            title: "Success",
            message: "审批拒绝",
            type: "success",
            duration: 2000,
          });
          this.getList();
          this.dialogFormVisible = false;
        });
      }
    },
    /*
     * 查看详情
     * */
    handleDetails(item) {
      this.$router.push({
        path: "/userDetails",
        query: { user_id: item.id_public_user_info },
      });
    },

    /*
     * 表格样式
     * */
    getSortClass: function (key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "ascending" : "descending";
    },
  },
};
</script>

<style lang="scss" scoped>
.el-select {
  ::v-deep {
    .el-select-dropdown__item {
      width: 200px;
    }
  }
}
</style>
